package first.endtoend.tsm.webservices;

import first.endtoend.tsm.TSMHelper;
import org.mbds.wolf.tsm.gp.systems.messaging2_1_0.GpFunctions;
import org.mbds.wolf.tsm.gp.systems.messaging2_1_0.IServiceEvents;
import org.mbds.wolf.tsm.gp.systems.messaging2_1_0.ksoap2.elements.OperationResult;
import org.mbds.wolf.tsm.gp.systems.messaging2_1_0.ksoap2.services.types.ExecutionStatusType;
import org.mbds.wolf.tsm.gp.systems.messaging2_1_0.ksoap2.services.types.ServiceIdentifierType;
import org.mbds.wolf.tsm.gp.systems.messaging2_1_0.ksoap2.services.types.ServiceInstanceReferenceType;
import org.mbds.wolf.tsm.gp.systems.messaging2_1_0.ksoap2.services.types.ServiceQualifierType;
import org.mbds.wolf.tsm.gp.systems.messaging2_1_0.ksoap2.services.types.requests.HandleEndServiceStateChangeNotificationRequestType;
import org.mbds.wolf.tsm.gp.systems.messaging2_1_0.ksoap2.services.types.requests.HandleStartServiceStateChangeNotificationRequestType;

import java.math.BigInteger;

/**
 * Created by ncordina on 23/06/2015.
 */
public class ServiceCycleNotification extends TSMHelper implements IServiceEvents
{
    /**
     * Class Constructor
     * Take the URL of SPTSM Server in parameter and instanciate the CallBack and the controller of Wolf library
     *
     * @param URL
     */
    public ServiceCycleNotification(String URL)
    {
        super(URL);
    }

    @Override
    public void Starting()
    {
        logger.debug("ServiceCycleNotification Starting");
    }

    @Override
    public synchronized void Completed(OperationResult operationResult)
    {
        if (operationResult.MethodName.equalsIgnoreCase(GpFunctions.HandleStartServiceStateChangeNotification.getFunction()))
        {
            logger.debug("HandleStartServiceStateChangeNotification Completed");
            notify();
        }
        if (operationResult.MethodName.equalsIgnoreCase(GpFunctions.HandleEndServiceStateChangeNotification.getFunction()))
        {
            logger.debug("HandleEndServiceStateChangeNotification Completed");
            notify();
        }
    }

    /**
     * @param operation
     * @return
     * @throws InterruptedException
     */
    public synchronized void handleSartServiceStateChange(String operation) throws InterruptedException
    {
        HandleStartServiceStateChangeNotificationRequestType handleStartServiceStateChangeNotificationRequestType = new HandleStartServiceStateChangeNotificationRequestType();
        handleStartServiceStateChangeNotificationRequestType.FunctionCallIdentifier = GpFunctions.HandleStartServiceStateChangeNotification.getFunction();
        handleStartServiceStateChangeNotificationRequestType.MobileSubscription = tools.initMobileSubcription();
        handleStartServiceStateChangeNotificationRequestType.SecureElement = tools.initSecureElement();
        handleStartServiceStateChangeNotificationRequestType.NewService = tools.initService();
        handleStartServiceStateChangeNotificationRequestType.NewServiceInstanceReference = tools.initServiceReferenceType();
        handleStartServiceStateChangeNotificationRequestType.NewServiceQualifier = tools.initServiceQualifier();
        handleStartServiceStateChangeNotificationRequestType.Operation = new BigInteger(operation);

        tsmController.HandleStartServiceStateChangeNotification(this, handleStartServiceStateChangeNotificationRequestType);
        wait();
    }

    /**
     * @param operation                1 => Service Deployment, 101 => Service Redeployment, 102 => Secure Element
     *                                 Application redeployment, 103 => Device Application redeployment, 2 => Service
     *                                 upgrade, 3 => Service update, 4 => Service Suspension, 5 => Service resumption,
     *                                 6
     *                                 => Service termination
     * @param currentServiceInstance
     * @param currentServiceIdentifier
     * @param currentServiceQualifier
     * @return
     * @throws InterruptedException
     */
    public synchronized void handleSartServiceStateChange(String operation, ServiceInstanceReferenceType currentServiceInstance, ServiceIdentifierType currentServiceIdentifier, ServiceQualifierType currentServiceQualifier) throws InterruptedException
    {
        HandleStartServiceStateChangeNotificationRequestType handleStartServiceStateChangeNotificationRequestType = new HandleStartServiceStateChangeNotificationRequestType();
        handleStartServiceStateChangeNotificationRequestType.FunctionCallIdentifier = GpFunctions.HandleStartServiceStateChangeNotification.getFunction();
        handleStartServiceStateChangeNotificationRequestType.ValidityPeriod = new BigInteger("60000");
        handleStartServiceStateChangeNotificationRequestType.MobileSubscription = tools.initMobileSubcription();
        handleStartServiceStateChangeNotificationRequestType.SecureElement = tools.initSecureElement();
        handleStartServiceStateChangeNotificationRequestType.CurrentService = currentServiceIdentifier;
        handleStartServiceStateChangeNotificationRequestType.CurrentServiceInstanceReference = currentServiceInstance;
        handleStartServiceStateChangeNotificationRequestType.CurrentServiceQualifier = currentServiceQualifier;
        handleStartServiceStateChangeNotificationRequestType.Operation = new BigInteger(operation);

        tsmController.HandleStartServiceStateChangeNotification(this, handleStartServiceStateChangeNotificationRequestType);
        wait();
    }

    /**
     * @param operation
     * @param executionStatusType
     * @param currentServiceInstance
     * @param currentServiceIdentifier
     * @param currentServiceQualifier
     * @throws InterruptedException
     */
    public synchronized void handleEndServiceStateChangeNotification(String operation, ExecutionStatusType executionStatusType, ServiceInstanceReferenceType currentServiceInstance, ServiceIdentifierType currentServiceIdentifier, ServiceQualifierType currentServiceQualifier) throws InterruptedException
    {
        HandleEndServiceStateChangeNotificationRequestType handleEndServiceStateChangeNotificationRequestType = new HandleEndServiceStateChangeNotificationRequestType();
        handleEndServiceStateChangeNotificationRequestType.FunctionCallIdentifier = GpFunctions.HandleEndServiceStateChangeNotification.getFunction();
        handleEndServiceStateChangeNotificationRequestType.ValidityPeriod = new BigInteger("60000");
        handleEndServiceStateChangeNotificationRequestType.MobileSubscription = tools.initMobileSubcription();
        handleEndServiceStateChangeNotificationRequestType.SecureElement = tools.initSecureElement();
        handleEndServiceStateChangeNotificationRequestType.CurrentService = currentServiceIdentifier;
        handleEndServiceStateChangeNotificationRequestType.CurrentServiceInstanceReference = currentServiceInstance;
        handleEndServiceStateChangeNotificationRequestType.CurrentServiceQualifier = currentServiceQualifier;
        handleEndServiceStateChangeNotificationRequestType.Operation = new BigInteger(operation);
        handleEndServiceStateChangeNotificationRequestType.OperationStatus = executionStatusType;

        //tsmController.HandleEndServiceStateChangeNotification(this, handleEndServiceStateChangeNotificationRequestType); //TODO Demander a Anne-Marie de coder la fonction
        wait();
    }

    public synchronized void handleEndServiceStateChangeNotification(String operation) throws InterruptedException
    {
        HandleEndServiceStateChangeNotificationRequestType handleEndServiceStateChangeNotificationRequestType = new HandleEndServiceStateChangeNotificationRequestType();
        handleEndServiceStateChangeNotificationRequestType.FunctionCallIdentifier = GpFunctions.HandleEndServiceStateChangeNotification.getFunction();
        handleEndServiceStateChangeNotificationRequestType.ValidityPeriod = new BigInteger("60000");
        handleEndServiceStateChangeNotificationRequestType.MobileSubscription = tools.initMobileSubcription();
        handleEndServiceStateChangeNotificationRequestType.SecureElement = tools.initSecureElement();
        handleEndServiceStateChangeNotificationRequestType.CurrentService = tools.initService();
        handleEndServiceStateChangeNotificationRequestType.CurrentServiceInstanceReference = tools.initServiceReferenceType();
        handleEndServiceStateChangeNotificationRequestType.CurrentServiceQualifier = tools.initServiceQualifier();
        handleEndServiceStateChangeNotificationRequestType.Operation = new BigInteger(operation);

        // tsmController.HandleEndServiceStateChangeNotification(this, handleEndServiceStateChangeNotificationRequestType); //TODO Demander a Anne-Marie de coder la fonction
        wait();
    }

}
